package com.softeem.service.Impl;

import com.softeem.entity.Info;
import com.softeem.entity.Staff;
import com.softeem.entity.StaffExample;
import com.softeem.entity.WorkOrder;
import com.softeem.mapper.InfoMapper;
import com.softeem.mapper.StaffMapper;
import com.softeem.mapper.WorkOrderMapper;
import com.softeem.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static javax.swing.UIManager.get;

@Service
public class StaffServiceImpl implements StaffService {

    @Autowired
    private WorkOrderMapper orderMapper;
    @Autowired
    private InfoMapper infoMapper;
    @Autowired
    private StaffMapper staffMapper;

    @Override
    public boolean takeOrder(WorkOrder workOrder) {
        try {
            return orderMapper.updateByPrimaryKeySelective(workOrder) > 0;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public boolean login(Staff staff, String isKeep, HttpServletRequest req, HttpServletResponse resp) {
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName()+","+cookie.getValue());
        }


        System.out.println(isKeep);
        StaffExample example = new StaffExample();
        example.or().andLoginNameEqualTo(staff.getLoginName());
        example.or().andPhoneEqualTo(staff.getLoginName());
        List<Staff> staffs = staffMapper.selectByExample(example);
        Staff staffRes =new Staff();
        if(staffs.size() == 0){
            staffRes = null;
        }else if(staffs.size() == 1){
            staffRes = staffs.get(0);
        }
        if(staffRes != null){
            if(staff.getPassword().equals(staff.getPassword())){
                Cookie loginNamecookie = new Cookie("loginName", staff.getLoginName());
                Cookie password = new Cookie("password",staff.getPassword() );
                Cookie isStaff = new Cookie("isStaff", "isStaff");

                loginNamecookie.setPath(req.getContextPath());
                password.setPath(req.getContextPath());
                isStaff.setPath(req.getContextPath());

                if("1".equals(isKeep)){
                    loginNamecookie.setMaxAge(60*60*24*30);
                    password.setMaxAge(60*60*24*30);
                    isStaff.setMaxAge(60*60*24*30);
                }else {
                    loginNamecookie.setMaxAge(0);
                    password.setMaxAge(0);
                    isStaff.setMaxAge(0);
                }
                resp.addCookie(loginNamecookie);
                resp.addCookie(password);
                resp.addCookie(isStaff);

                req.getSession().setAttribute("staffInfo",staffRes);
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

}
